import javax.xml.soap.Node;

public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    TreeNode root = null;

    public boolean nodeSearch(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val > key) {
                cur = cur.left;
            }else if (cur.val < key) {
                cur = cur.right;
            }else {
                return true;
            }
        }
        return false;
    }

    public boolean insert(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        TreeNode a = new TreeNode(key);
        if (cur == null) {
            TreeNode root = a;
            return true;
        }

        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;

            }else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else {
                return false;
            }
        }

        if (parent.val > key) {
            parent.left = a;
        }else {
            parent.right = a;
        }
        return true;
    }

    public void remove(int key) {
        TreeNode cur = root;
        TreeNode parent = root;
        //循环找到要删除的结点
        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            }else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            }else {
                //删除逻辑单独处理
                removeNode(parent,cur);
                return;
            }
        }
    }
    public void removeNode(TreeNode parent,TreeNode cur) {

        //三种情况，三种情况内部又存在多种特殊情况
        //1.删除结点是叶子结点无左右子树
        if (cur.left == null && cur.right == null) {
            if (cur == root) {
                root = null;
            }else if (parent.val > cur.val) {
                parent.left = null;
            }else {
                parent.right = null;
            }
        //2.删除的结点有左右子树之一
        }else if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            }else if (parent.left == cur) {
                parent.left = cur.right ;
            }else if (parent.right == cur){
                parent.right = cur.right;
            }
        }else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            }else if (parent.left == cur) {
                parent.left = cur.left ;
            }else if (parent.right == cur){
                parent.right = cur.left;
            }
        //3.删除的结点左右子树都存在
        }else {
            TreeNode targetParent = cur;
            TreeNode target = cur.left;
            //！！！！会出现特殊情况，即未进入循环
            while(target.right != null) {
                targetParent = target;
                target = target.right;
            }
            /*由于循环内：
             targetParent = target;
             target = target.right;
            正常情况下target为targetParent的右结点，但是如果没进入循环：
             TreeNode targetParent = cur;
             TreeNode target = cur.left;*/
            //！！！两种易错情况
            cur.val = target.val;
            if (target == targetParent.left) {
                targetParent.left = target.left;
            } else {
                targetParent.right = target.left;
            }

        }
    }
}
